package com.atguigu.auth.service.impl;

import com.atguigu.auth.mapper.SysUserMapper;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.model.system.SysUser;
import com.atguigu.security.custom.LoginUserInfoHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-04-15
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    /**
     * 更新用户状态
     *
     * @param id     {@link Long}
     * @param status {@link Integer}
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        // 根据 userid 查询用户对象
        SysUser sysUser = getById(id);
        // 设置修改状态值
        sysUser.setStatus(status);
        // 调用方法进行修改
        updateById(sysUser);
    }

    /**
     * 根据用户名进行查询
     *
     * @param username {@link String}
     * @return {@link SysUser}
     */
    @Override
    public SysUser getUserByUserName(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, username);
        return getOne(wrapper);
    }

    /**
     * 获取当前用户
     *
     * @return {@link Map<String, Object>}
     */
    @Override
    public Map<String, Object> getCurrentUser() {
        SysUser sysUser = getById(LoginUserInfoHelper.getUserId());
        Map<String, Object> map = new HashMap<>(2);
        map.put("name", sysUser.getUsername());
        map.put("phone", sysUser.getPhone());
        return map;
    }
}
